package com.jianz.campus_markets.controller;


import com.jianz.campus_markets.domain.ResponseResult;
import com.jianz.campus_markets.domain.delay.DelayedOrder;
import com.jianz.campus_markets.domain.entity.Order;
import com.jianz.campus_markets.service.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author jianz
 * @since 2023-11-26
 */
@RestController
@RequestMapping("/order")
@Api(value = "订单模块")
public class OrderController {

    @Autowired
    private OrderService orderService;

    /**
     * 创建订单
     * */
    @PostMapping("/create")
    @ApiOperation("创建订单")
    public ResponseResult createOrder(@RequestBody Order order) {
        DelayedOrder delayedOrder = orderService.createOrder(order);
        return ResponseResult.okResult(delayedOrder.getValue());
    }

    /**
     * 支付订单
     * */
    @GetMapping("/pay")
    @ApiOperation("支付订单")
    public ResponseResult payOrder(@RequestParam("orderId")String orderId) {
        boolean ret = orderService.payOrder(orderId);
        return ResponseResult.okResult(ret);
    }

    @GetMapping("/update")
    @ApiOperation("此为内部调用接口，作用为订单过期时改变订单状态(监听器里使用update命令不执行sql，不知道为啥，可能是锁住了)")
    public ResponseResult update(String orderId) {
        orderService.autoCancelOrder(orderId);
        return ResponseResult.okResult();
    }

    @GetMapping("/getOrderInfo")
    @ApiOperation("获取订单信息")
    public ResponseResult getOrderInfo(int userId) {

        return ResponseResult.okResult(orderService.getOrderInfo(userId));
    }

}

